package ui;

import Main.Main;
import util.GsonUils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

/**
 * @author zhongwen
 * @description 图标宽高选择框
 * @date 2021-12-28 14:48
 */
public class IconDialog extends JDialog {
    private JPanel contentPanel = new JPanel();
    private int selectedSize;
    private final JLabel iconLabel = new JLabel("图片宽高选择器", SwingConstants.CENTER);
    private final JComboBox<Integer> checkBox = new JComboBox<>();
    public IconDialog(Frame owner, String title, boolean modal) {
        super(owner, title, modal);
        selectedSize = Main.configurationBean.getImageWidthAndHeight();
        this.setContentPane(contentPanel);
        this.setSize(400,220);
        this.setLocationRelativeTo(null);
        contentPanel.setLayout(null);
        contentPanel.add(iconLabel);
        iconLabel.setBounds(100,10,180,40);
        for(int i = 8; i <= 40; i++){
            checkBox.addItem(i);
        }
        checkBox.setSelectedItem(selectedSize);
        checkBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                selectedSize = (Integer) e.getItem();
            }
        });
        JLabel whl = new JLabel("宽高：");
        contentPanel.add(whl);
        whl.setBounds(130,60,80,40);
        contentPanel.add(checkBox);
        checkBox.setBounds(180,60,80,40);
        JButton okButton = new JButton("确定");
        JButton cancelButton = new JButton("取消");
        contentPanel.add(okButton);
        contentPanel.add(cancelButton);
        okButton.setBounds(100,120,100,30);
        cancelButton.setBounds(200,120,100,30);
        okButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Main.configurationBean.setImageWidthAndHeight(selectedSize);
                GsonUils.objectToString(Main.configurationBean);
                IconDialog.this.dispose();
            }
        });
        cancelButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                IconDialog.this.dispose();
            }
        });
    }
}
